package com.shensi.adminCollect.controller.applet;

import com.shensi.adminCollect.domain.applet.QuerySummaryRequest;
import com.shensi.adminCollect.domain.applet.QuerySummaryResponse;
import com.shensi.adminCollect.service.applet.impl.QuerySummaryService;
import com.shensi.common.core.controller.BaseController;
import com.shensi.common.core.domain.AjaxResult;
import com.shensi.common.core.domain.model.LoginUser;
import com.shensi.common.core.page.TableDataInfo;
import com.shensi.adminCollect.domain.customer.CustomerCardUseRecord;
import com.shensi.adminCollect.service.customer.ICustomerCardUseRecordService;
import com.shensi.adminCollect.domain.station.PayOrder;
import com.shensi.adminCollect.domain.station.Station;
import com.shensi.adminCollect.domain.station.StationOillGun;
import com.shensi.adminCollect.service.station.IPayOrderService;
import com.shensi.adminCollect.service.station.IStationService;
import com.shensi.common.core.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.shensi.common.core.domain.model.LoginUser;
import com.shensi.common.core.utils.SecurityUtils;
import com.shensi.common.core.domain.entity.SysUser;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 优惠劵Controller
 *
 * @author qujia
 * @date 2021-08-09
 */
@Api("首页查询")
@RestController
@RequestMapping("/applet")
public class QuerySummaryController extends BaseController
{
    @Autowired
    private QuerySummaryService querySummaryService;

    @Autowired
    private IStationService stationService;

    @Autowired
    private IPayOrderService payOrderService;

    @Autowired
    private ICustomerCardUseRecordService customerCardUseRecordService;
/*    @Autowired
    private QuerySummaryService couponInfoService;


    @Autowired
    private ISysEnterpriseUserService sysEnterpriseUserService;
    @Autowired
    private ISysStationUserService sysStationUserService;*/

    /**
     * 查询优惠劵列表
     */
    @ApiOperation("首页查询")
    @ApiImplicitParam(name = "list", value = "首页查询", dataType = "CouponInfo")
    @GetMapping("/summary")
    public AjaxResult list(QuerySummaryRequest request) {
        if(request.getLevel() == null || request.getStartTime() == null || request .getEndTime() == null){
            return AjaxResult.error("必填参数为空");
        }
        if(("1".equals(request.getLevel()) && request.getEnterpriseId() == null )){
            return AjaxResult.error("必填参数为空");
        }
        if(("2".equals(request.getLevel()) && request.getStationId() == null)){
            return AjaxResult.error("必填参数为空");
        }

//        if("1".equals(request.getLevel())){
//            List<Integer> stationIdList = querySummaryService.selectStationIdListByEnterpriseId(request.getEnterpriseId());
//            return AjaxResult.success(stationIdList);
//        }
        Map<String ,Object> resultMap = new HashMap<>();     //返回结果集
        QuerySummaryResponse sumData = querySummaryService.selectSumData(request);
        List<QuerySummaryResponse> oilGroupData = querySummaryService.selectSumOilGroupData(request);
        List<QuerySummaryResponse> echartData = querySummaryService.selectSumEchartData(request);
        resultMap.put("sumData", sumData);
        resultMap.put("oilGroupData", oilGroupData);
        resultMap.put("echartData", echartData);



//        resultMap.put("stationName", station.getStationName());

//
//        for (Map.Entry<String, String> entry : params.entrySet()) {
//            logger.info("Key = " + entry.getKey() + ", Value = " + entry.getValue());
//        }
//        return AjaxResult.success();
        return AjaxResult.success(resultMap);
    }
    /**
     * 查询所有订单包括未支付的
     */

    @GetMapping("/orderList")
    public TableDataInfo allList(PayOrder payOrder) {
        List<String> list = new ArrayList<>();
        LoginUser current = SecurityUtils.getLoginUser();

        if ("1".equals(current.getUser().getUserLevel())) {
            Station station = new Station();
            station.setEnterpriseId(current.getUser().getEnterpriseId());
            List<Station> stations = stationService.selectStationList(station);

            for (Station ss : stations) {
                list.add(ss.getStationId() + "");
            }
            if (stations != null && stations.size() > 0) {
                payOrder.setStationIdList(list);
            }

        } else if ("2".equals(current.getUser().getUserLevel())) {
            payOrder.setStationId(current.getUser().getStationId());
        } else {

        }
        startPage();
        List<PayOrder> payOrderList = payOrderService.selectAllPayOrderList(payOrder);
        return getDataTable(payOrderList);
    }

    @GetMapping("/gunlist")
    public AjaxResult gunlist(@ApiParam(value = "用户名", required = false) String userId) {

        LoginUser current = SecurityUtils.getLoginUser();
        SysUser user = current.getUser();
        if(user == null || !"2".equals(user.getUserLevel())){
            return AjaxResult.error("只开放给油站级别用户");
        }
        String gunStr = user.getGun();
        if(StringUtils.isBlank(gunStr)){
            return AjaxResult.error("该用户未绑定油枪");
        }
        StationOillGun staOilGun = new StationOillGun();

        staOilGun.setGunNameList(gunStr.split(","));
        staOilGun.setStationId(user.getStationId());

        List<StationOillGun> stationOillGuns = querySummaryService.selectGunlist(staOilGun);


        return AjaxResult.success(stationOillGuns);
    }

    @GetMapping("/appletOrderSum")
    public AjaxResult appletOrderSum(PayOrder payOrder) {
        List<String> list = new ArrayList<>();
        LoginUser current = SecurityUtils.getLoginUser();
        if ("1".equals(current.getUser().getUserLevel())) {
            Station station = new Station();
            station.setEnterpriseId(current.getUser().getEnterpriseId());
            List<Station> stations = stationService.selectStationList(station);

            for (Station ss : stations) {
                list.add(ss.getStationId() + "");
            }
            if (stations != null && stations.size() > 0) {
                payOrder.setStationIdList(list);
            }

        } else if ("2".equals(current.getUser().getUserLevel())) {
            payOrder.setStationId(current.getUser().getStationId());
        } else {

        }
        startPage();
        PayOrder payOrderList = payOrderService.selectAppletPayOrderSum(payOrder);
        CustomerCardUseRecord  customerCardUseRecord =new CustomerCardUseRecord();
        customerCardUseRecord.setBeginTime(payOrder.getStartTime());
        customerCardUseRecord.setEndTime(payOrder.getEndTime());
        customerCardUseRecord.setStatus("1");

        if ("1".equals(current.getUser().getUserLevel())) {
            Station station = new Station();
            station.setEnterpriseId(current.getUser().getEnterpriseId());
            List<Station> stations = stationService.selectStationList(station);

            for (Station ss : stations) {
                list.add(ss.getStationId() + "");
            }
            if (stations != null && stations.size() > 0) {
                customerCardUseRecord.setStationIdList(list);
            }

        } else if ("2".equals(current.getUser().getUserLevel())) {
            customerCardUseRecord.setStationId(Math.toIntExact(current.getUser().getStationId()));
        }

        CustomerCardUseRecord customerCardUseRecord1=customerCardUseRecordService.appletCzSum(customerCardUseRecord);
        if(customerCardUseRecord1!=null){
            payOrderList.setCzAmt(customerCardUseRecord1.getCzAmt());
            payOrderList.setWxCzAmt(customerCardUseRecord1.getWxAmt());
            payOrderList.setImpCzAmt(customerCardUseRecord1.getImpAmt());
        }
        return AjaxResult.success(payOrderList);
    }



}
